<template>
    <!-- 服务端导出 -->
    <span>
        <a-button v-if="isButton" v-bind="$attrs" v-on="$listeners" :type="buttonType" :icon="icon" @click="downFile">{{
            buttonText
        }}</a-button>
        <!-- 插槽 -->
        <a v-else v-bind="$attrs" v-on="$listeners" :type="buttonType" :icon="icon" @click="downFile">{{
            buttonText
        }}</a>
    </span>
</template>

<script>
import { unionApiUrl } from '@/api/config.js'


import Vue from 'vue'
export default {
    inheritAttrs: false,
    name: 'ExportMould',
    props: {
        // 导出模板按钮文本
        buttonText: {
            type: String,
            default: () => '导出'
        },
        // 导出按钮类型
        buttonType: {
            type: String,
            default: () => 'primary'
        },
        // // 自定义上传容器
        // custom: {
        //     type: Boolean,
        //     default: () => false,
        // },
        isButton: {
            type: Boolean,
            default: () => true
        },
        // 导出按钮图标
        icon: {
            type: String,
            default: () => ''
        },
        //导出api所属服务
        serviceiUrl: {
            type: String,
            default: () => unionApiUrl
        },
        //导出的api地址
        url: {
            type: String,
            default: () => ''
        },
        //导出请求参数
        requestParams: {
            type: Object,
            default() {
                return {}
            }
        }
    },
    data() {
        return {
            token: ''
        }
    },
    mounted() {
        this.token = Vue.getAcloudProvider().getAccessToken()
    },
    methods: {
        jointUrl(url, params) {
            let idx = url.indexOf('?')
            if (idx < 0) {
                url = url + '?'
            } else if (idx !== url.length - 1) {
                if (url.charAt(url.length - 1) !== '&') {
                    url = url + '&'
                }
            }
            for (let key in params) {
                if (params.hasOwnProperty(key)) {
                    let v = params[key]
                    if (v !== '') {
                        url += key + '=' + params[key] + '&'
                    }
                }
            }
            return url
        },
        /**
         * 下载模板
         */
        downFile() {
            this.$emit('beforeDownload')
            const link = document.createElement('a')

            link.href = this.jointUrl(
                `/api${this.serviceiUrl}${this.url}?access_token=${this.token}`,
                this.requestParams
            )
            link.setAttribute('target', '_self')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
        },
        downFilePost() {
            link.href = this.jointUrl(
                `/api${this.serviceiUrl}${this.url}?access_token=${this.token}`,
                this.requestParams
            )
            return axios.post(url, data, { responseType: 'arraybuffer' }).then(function (response) {
                let blob = new Blob([response.data], { type: 'application/xls' })
                let link = document.createElement('a')
                link.href = window.URL.createObjectURL(blob)
                link.download = fileName ? fileName : '标准模板下载' + '.xlsx'
                link.click()
            })
        },
        // downloadGet(url, data, params = {}) {
        //     link.href = this.jointUrl(
        //         `/api${this.serviceiUrl}${this.url}?access_token=${this.token}`,
        //         this.requestParams
        //     )
        //     return axios.get(url, { params, responseType: 'arraybuffer' }).then(function (response) {
        //         let blob = new Blob([response.data], { type: 'application/xls' })
        //         let link = document.createElement('a')
        //         link.href = window.URL.createObjectURL(blob)
        //         link.download = this.filename + '.xls'
        //         link.click()
        //     })
        // }
    }
}
</script>